package com.controller;


import com.common.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Product;
import com.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品后台", value = "商品后台")
@CrossOrigin
@RestController
public class ProductAdminController {

    @Autowired
    ProductService productService;

    @ApiOperation(value = "分页查询全部商品", httpMethod = "GET")
    @GetMapping(value = "/api/selectAllProductsByPage")
    public Result selectAllProducts(
            @RequestParam(value = "pageNum", defaultValue = "1",required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5", required = false) Integer pageSize
    ){
        PageHelper.startPage(pageNum, pageSize);
        List<Product> products = productService.selectAllProducts();
        PageInfo<Product> pageInfo = new PageInfo<Product>(products);
        return Result.seccess(200, "查询成功", pageInfo);
    }

    @ApiOperation(value = "添加商品", httpMethod = "POST")
    @PostMapping(value = "/api/addProduct")
    public Result addProduct(@RequestBody Product product){
        int i = productService.addProduct(product);
        return Result.seccess(200, "插入成功", i);
    }

    @ApiOperation(value = "根据ID删除商品", httpMethod = "DELETE")
        @DeleteMapping(value = "/api/deleteProductById")
    public Result deleteProductById(@RequestParam(value = "id", required = false) Integer id){
        int i = productService.deleteProductById(id);
        return Result.seccess(200, "删除成功", i);
    }

    @ApiOperation(value = "根据ID修改商品", httpMethod = "PUT")
    @PutMapping(value = "/api/updateProductById")
    public Result updateProductById(@RequestBody Product pro){
        int i = productService.updateProductById(pro);
        return Result.seccess(200, "修改成功", i);
    }

}
